﻿using System;
using System.Runtime.Caching;
using System.Threading.Tasks;
using WS.EKA.DataAccess;
using WS.EKA.Utilities;
using WS.EKA.Utilities.Consts;
using WS.EKA.Utilities.Extensions;

namespace WS.EKA.Core.Initial
{
    public class DataInitial
    {
        public static void LoadInitialDataToMemeory()
        {
            Task.Factory.StartNew(() => { LoadCatagoryToMemeory(); });
            Task.Factory.StartNew(() => { LoadBaskOrderLogToMemeory(); });
            Task.Factory.StartNew(() => { LoadCommentsToMemeory(); });
            Task.Factory.StartNew(() => { LoadProductPropertyToMemory(); });
            Task.Factory.StartNew(() => { LoadProductsToMemeory(); });
            Task.Factory.StartNew(() => { LoadPackToMemory(); });
            Task.Factory.StartNew(() => { LoadBlessCardToMemory(); });
        }

        private static CacheItemPolicy CacheItemPolicy
        {
            get
            {
                CacheItemPolicy policy = new CacheItemPolicy();
                policy.AbsoluteExpiration = DateTime.Now.AddHours(int.Parse(UserConsts.CacheExpiredTime.ConfigValue()));
                policy.RemovedCallback += CachedItemRemovedCallback;
                return policy;
            }

        }
        private static void LoadProductsToMemeory()
        {
            var products = ProductContext.GetByConditions(null);
            CacheManager.Instance.InsertByMemoryCache(CacheKey.Product, products, CacheItemPolicy);
        }

        private static void LoadCommentsToMemeory()
        {
            var comments = ProductCommentContext.GetByConditions(null);
            CacheManager.Instance.InsertByMemoryCache(CacheKey.ProductComment, comments, CacheItemPolicy);
        }

        private static void LoadCatagoryToMemeory()
        {
            var catagories = ProductCatagoryContext.GetByConditions(null);
            CacheManager.Instance.InsertByMemoryCache(CacheKey.ProductCatagory, catagories, CacheItemPolicy);
        }

        private static void LoadProductPropertyToMemory()
        {
            var properties = ProductPropertyContext.GetByConditions(null);
            CacheManager.Instance.InsertByMemoryCache(CacheKey.ProductProperty, properties, CacheItemPolicy);
        }

        private static void LoadBaskOrderLogToMemeory()
        {
            var logs = BaskOrderLogContext.GetByConditions(null);
            CacheManager.Instance.InsertByMemoryCache(CacheKey.BaskOrderLog, logs, CacheItemPolicy);
        }

        private static void LoadPackToMemory()
        {
            var packs = PackContext.GetByConditions(null);
            CacheManager.Instance.InsertByMemoryCache(CacheKey.Pack, packs, CacheItemPolicy);
        }

        private static void LoadBlessCardToMemory()
        {
            var blessCards = BlessCardContext.GetByConditions(null);
            CacheManager.Instance.InsertByMemoryCache(CacheKey.BlessCard, blessCards, CacheItemPolicy);
        }

        private static void CachedItemRemovedCallback(CacheEntryRemovedArguments arguments)
        {
            switch (arguments.CacheItem.Key)
            {
                case CacheKey.Product:
                    LoadProductsToMemeory();
                    break;
                case CacheKey.ProductComment:
                    LoadCommentsToMemeory();
                    break;
                case CacheKey.ProductCatagory:
                    LoadCatagoryToMemeory();
                    break;
            }
        }
    }
}
